// packageI/othersPage/othersPage.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let language = wx.getStorageSync('langIndex');
    this.setData({
      'language': language.en
    })
    if (this.options.topic_id) {
      this.setData({
        topic_id: options.topic_id
      })
      this.getinfo();
      this.getData();
    }

  },
  toGoods(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?id=' + id,
    })
  },
  getinfo() {
    let urlStr = app.getNetAddresss('plugin.project-template.frontend.topic.getTopicByTopicId');
    app._postNetWork({
      url: urlStr,
      data: {
        topic_id: this.data.topic_id
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            info: res.data,
          });
          wx.setNavigationBarTitle({
            title: this.data.info.title,
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1000
          });
        }
      },
      fail: function (res) {}
    });
  },
  getData() {
    let urlStr = app.getNetAddresss('plugin.project-template.frontend.topic.getTopicGoodsByTopicId');
    app._postNetWork({
      url: urlStr,
      data: {
        page: this.data.page,
        topic_id: this.data.topic_id
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          let _data = res.data;
          this.setData({
            isLoadMore: true,
            total_page: _data.last_page,
            list: _data.data
          });
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
          if (this.data.page >= this.data.total_page) {
            this.setData({
              isLoadMore: false
            });
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1000
          });
        }
      },
      fail: function (res) {}
    });
  },
  _getMoreData() {
    if (this.data.page >= this.data.total_page) {
      this.setData({
        isLoadMore: false
      });
      wx.hideLoading({
        success: (res) => {},
      })
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      });
      let urlStr = app.getNetAddresss('plugin.find-point-reward.frontend.controllers.center');
      app._postNetWork({
        url: urlStr,
        data: {
          page: this.data.page,
          topic_id: this.data.topic_id
        },
        success: (resdata) => {
          let res = resdata.data;
          if (res.result == 1) {
            let _data = res.data;
            let myData = this.data.list.concat(res.data);
            this.setData({
              isLoadMore: true,
              total_page: _data.last_page,
              list: myData
            });
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 1000
            });
          }
        },
        fail: function (res) {}
      });
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})